<body>

	<div class="warp h">
		<!--header-->
		<header class="hd br">
			<?php if(! isset($goodsid)): ?>
            	<a href="<?php echo $base_url; ?>home/goods/index" class="l-icon only-icon">
            <?php else:?>
        		<a href="<?php echo $base_url; ?>home/goods/goodsDetail?goods_id=<?php echo $goodsid; ?>" class="l-icon only-icon">
        	<?php endif ?>
            <i class="iconfont icon-left1"></i>            
            
        </a>
			<p class="tit2">
				购物车
			</p>
		</header>

		<ul class="order-list">
			<!--<div style="width:.66rem;height:.66rem;border-radius:50%;background-color:red"></div>-->

		</ul>
	<div class="line-box"></div>
		<ul class="order-list-sx">
			
		</ul>
		<div class="line-box"></div>

		<div class="pd-box"></div>
		<div class="sub-order-ft">

			<div class="pricesum">
				<div class="icon-box all-btn">
					<i class="iconfont myicon "></i>
					<span>全选</span>
				</div>
				合计: <span class="sumPrice">¥0.00</span>
			</div>
			<a class="sub-btn">
                去结算
            </a>
		</div>
		
	</div>

	<script src="/static/dist/js/base.js"></script>
	<!--<script src="/static/dist/js/lib/store.legacy.min.js"></script>-->
	<script>
		// 矫正rem
		function correctrem() {
			var rem_full_width = $("<div id='rem-test' style='width:7.5rem'></div>").appendTo($('body')).width();
			var device_width = Math.min(document.documentElement.clientWidth, 640);
			if (rem_full_width !== device_width) {
				document.documentElement.style.fontSize = (device_width / 7.5) * (device_width / rem_full_width) + 'px';
			}
			$('#rem-test').remove();
		}
		correctrem();

		$(function () {
			$(".order-list").on("click", ".order-box-5 .icon-box", function (ev) {
				var $this_i = $(this).find('.icon-box i');
				if (!$this_i.hasClass("icon-gou")) {
					// $(".icon-box i").removeClass("icon-gou");
					$this_i.addClass("icon-gou");
				} else {
					$this_i.removeClass("icon-gou");
				}
				var bs = false;
				var len = $(".order-box-5").find('.icon-box i').length
				var len2 = $(".order-box-5").find('.icon-gou').length
				len == len2 ? $(".all-btn").find(".icon-box i").addClass("icon-gou") : $(".all-btn").find(".icon-box i").removeClass("icon-gou")

			});
			$(".all-btn").hammer().bind("tap", function (ev) {
				var $this_i = $(this).find('.icon-box i');
				if (!$this_i.hasClass("icon-gou")) {
					// $(".icon-box i").removeClass("icon-gou");
					$(".order-box-5").find(".icon-box i").addClass("icon-gou");
					$this_i.addClass("icon-gou");
				} else {
					$(".order-box-5").find(".icon-box i").removeClass("icon-gou");

					$this_i.removeClass("icon-gou");
				}
			});
		})
	</script>
	<script>
		$(function () {
			var cart = getCookie('cartData') || [];			

			var total_price = 0;
			var html = "";
			if(cart.length >0){ 
			$.ajax({
						url:"<?php echo $base_url;?>ajax/goods/getDetail",
						type:'POST',
						data:{'cartData':cart},
						dataType:'json',
						success:function(result){
							var effect =  result.data.effect,
							noeffect = result.data.noeffect;
							html = "";
							html_sx = "";
							if(effect.length > 0){
								for(var i=0;i<effect.length;i++){
									html += '<li>'+
			                        '<div class="line-box"></div>'+
									'<div class="order-itme" data-effect="1" data-id="'+ effect[i]['goods_id'] + '" data-price="' + effect[i]['price'] +'">'+
									'<div class="order-box-5">'+
									'<div class="icon-box">'+'<i class="iconfont myicon "></i></div>'+
									'<a class="cont" href="/home/goods/goodsDetail?from=shop_car&goods_id='+ effect[i]['goods_id'] + '">'+			
									'<div class="l"><img src="<?php echo $base_url;?>static/upload/goods/' + effect[i]['img_path'] + '" alt="' + effect[i]['img_name'] + '"></div>'+
									'<div class="r"><p class="tit">' + effect[i]['name'] + '</p> <p class="price-num"><span>¥</span>' + effect[i]['price'] + '</p></div>'+
										// <p class="ms-tit">颜色分类：默认；</p>
									'</a>'+
									'<span class="del-btn"><i class="iconfont icon-lajitong"></i></span><ul class="num-js"><li class="sub"><i class="iconfont icon-jian"></i></li><li class="num">' + effect[i]['amount'] + '</li><li class="add"><i class="iconfont icon-jia"></i></li></ul></div></div></li>';
								}
								$(".order-list").append(html);
							}
							if(noeffect.length > 0){
								
								for(var i=0;i<noeffect.length;i++){
									html_sx += '<li>'+
									'<div class="order-itme" data-effect="0" data-id="'+ noeffect[i]['goods_id'] + '" data-price="' + noeffect[i]['price'] + '">'+
									'<div class="order-box-5">'+
									'<div class="icon-box2">'+
									'<span class="sx">失效</span>'+
									'</div>'+
									'<a class="cont" >'+
									'<div class="l">'+
									'<img src="<?php echo $base_url;?>static/upload/goods/' + noeffect[i]['img_path'] + '" alt="' + noeffect[i]['img_name'] + '"></div>'+
									'<div class="r"><p class="tit">' + noeffect[i]['name'] + '</p> <p class="price-num"><span>¥</span>' + noeffect[i]['price'] + '</p></div>'+
									'</a>'+
									'<span class="del-btn"><i class="iconfont icon-lajitong"></i></span>'+
									'<ul class="num-js">'+
										'<li class="sub">-</li>'+
										'<li class="num">' + noeffect[i]['amount'] + '</li>'+
										'<li class="add">+</li>'+
									'</ul>'+
									'</div>'+
									'</div>'+
									'</li>';
								}
								
								$(".order-list-sx").append(html_sx);
							}
							
							
						}

					})
			}else{
				html = '<div class="none-car"><img src="/static/dist/images/nonecar.jpg" alt=""></div>';
				$(".warp").append(html);
				$(".sub-order-ft").css('display','none');//不显示底边栏
			}


			//  $(".pricesum").append("<span>￥ "+total_price+"</span>");
			// ----------------------------------------------------------
			var newitemData = {
				"id": 0,
				"price": 0,
				"quantity": 0
			};
			var cartData = getCookie('cartData') || [];
			var maxVal = 10000, minVla = 1, sumPrice = 0;
			$('.order-list').on('click', '.num-js .add', function () {
				var $num = $(this).parents('.num-js').find('.num');
				var $sel = $(this).parents('.order-itme').find('.myicon')
				var isSel = false, isCC = false;
				if ($(this).parents('.order-itme').find('.icon-gou').length) { isSel = true; }
				var id = $(this).parents('.order-itme').data('id');
				var price = $(this).parents('.order-itme').data('price');
				var num = parseInt($num.text());
				num++;
				if (num > maxVal) {
					num = maxVal;
					isCC = true;
				}

				$num.text(num)



				for (var i = 0; i < cartData.length; i++) {
					if (cartData[i].id == id) {
						if (cartData[i].quantity) {
							cartData[i].quantity = num;
						} else {
							newitemData.quantity = num;
							newitemData.id = id;
							newitemData.price = price
							cartData[i] = itemData
						}
					}
				}
				setCookie('cartData',cartData,2)
				// store.set('cartData', cartData);
				if (isSel) {
					if (!isCC) {
						sumPrice += parseFloat(price) // * num;					
						$(".sumPrice").text("¥" + parseFloat(sumPrice).toFixed(2))
					}
				}


			})
			$('.order-list').on('click', '.num-js .sub', function () {
				var $num = $(this).parents('.num-js').find('.num');
				var $sel = $(this).parents('.order-itme').find('.myicon')
				var isSel = false, isCC = false;
				if ($(this).parents('.order-itme').find('.icon-gou').length) { isSel = true; }
				var num = parseInt($num.text());

				var id = $(this).parents('.order-itme').data('id');
				var price = $(this).parents('.order-itme').data('price');
				num--;
				if (num < minVla) {
					num = minVla;
					isCC = true;
				}
				$num.text(num)

				for (var i = 0; i < cartData.length; i++) {
					if (cartData[i].id == id) {
						if (cartData[i].quantity) {
							cartData[i].quantity = num;
						} else {
							itemData.quantity = num;
							itemData.id = id;
							itemData.price = price
							cartData[i] = itemData
						}
					}
				}
				// store.set('cartData', cartData)
				setCookie('cartData',cartData,2)
				if (isSel) {
					if (!isCC) {
						sumPrice -= parseFloat(price) // * num;					
						$(".sumPrice").text("¥" + parseFloat(sumPrice).toFixed(2))
					}
				}

			})

			$(".order-list").on("click", ".order-box-5 .icon-box", function () {
				var $sel = $(this).find('.myicon');
				var $item = $(this).parents('.order-itme');
				var itemData = getcurrentCartData($item.data('id'));
				// console.log(itemData)
				if ($sel.hasClass('icon-gou')) {
					sumPrice += parseFloat(itemData.price) * parseInt(itemData.quantity);
					// sumPrice += parseFloat(price)
				} else {
					sumPrice -= parseFloat(itemData.price) * parseInt(itemData.quantity);
					// sumPrice += parseFloat(price)					
				}
				$(".sumPrice").text("¥" + parseFloat(sumPrice).toFixed(2))

			})

			$(".all-btn").hammer().bind("tap", function (ev) {
				var $sel = $(this).find('.myicon');
				var $item = $(".order-itme[data-effect='1']");
				sumPrice = 0;
				if ($sel.hasClass('icon-gou')) {
					$item.each(function (i, el) {
						var itemData = getcurrentCartData($(el).data('id'));
						sumPrice += parseFloat(itemData.price) * parseInt(itemData.quantity);
					})
					$(".sumPrice").text("¥" + parseFloat(sumPrice).toFixed(2))
				} else {
					$(".sumPrice").text("¥0.00")

				}
			})

			$('body').on('click','.del-btn',function(){
				var $this = $(this);				
				var id = $this.parents('.order-itme').data('id');
				var newCartData = []
				if($this.parents('.order-itme').find('.icon-gou').length){					
					var itemData = getcurrentCartData($this.parents('.order-itme').data('id'));	
					sumPrice -= parseFloat(itemData.price) * parseInt(itemData.quantity);										
					$(".sumPrice").text("¥" + parseFloat(sumPrice).toFixed(2))							
				}
				$this.parents('.order-itme').remove();

				for(var i = 0; i<cartData.length;i++){					
					if(cartData[i].id != id){
						newCartData.push(cartData[i])
					}					
				}
				cartData = newCartData;				
				// store.set('cartData', newCartData);
				setCookie('cartData',newCartData,2)
				if($('.order-itme').length == 0){
					var html = '<div class="none-car"><img src="/static/dist/images/nonecar.jpg" alt=""></div>';
					$('.warp').append(html);
					$('.pricesum').remove();
					$('.sub-order-ft').remove();
				}
				
			})
			

			function getcurrentCartData(id) {
				var result = {};
				if (cartData && cartData.length != 0) {
					for (var i = 0; i < cartData.length; i++) {
						if (cartData[i].id == id) {
							result = cartData[i];
						}
					}
				}
				return result;
			}

			$(".sub-btn").click(function(){
				var selArr=[];
				$(".order-itme").find(".icon-gou").each(function(i, item){
					var selObj = {};
					selObj.goods_id = $(item).parents(".order-itme").data("id");					
					selObj.quantity = $(item).parents(".order-itme").find(".num-js .num").text()
					selArr.push(selObj);
				})
				if(selArr.length <= 0)
				{
					alert('请选择至少一件商品');
				}
				else
				{
			 		location.href = "<?php echo $base_url;?>home/order/submitorder?data=" + encodeURI(JSON.stringify(selArr));
				}
			});
			
			function setCookie(cname, cvalue, exdays) {
				cvalue = JSON.stringify(cvalue)

				var d = new Date();
				d.setTime(d.getTime() + (exdays*60*60*1000));
				var expires = "expires="+d.toUTCString();
				document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";  
			}
			function getCookie(cname) {
				var name = cname + "=";
				var ca = document.cookie.split(';');
				for(var i=0; i<ca.length; i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1);
					if (c.indexOf(name) != -1){
						return JSON.parse(c.substring(name.length, c.length));
					} 
				}
				return "";
			}
		});

		
	</script>
</body>

</html>
